home *** CD-ROM | disk | FTP | other *** search
- /* Application C template source file
- David A. Feldt - version 1.2 - 8/12/85
-
- Copyright © 1985 by David A. Feldt. All rights reserved.
-
- This template is based on a Pascal example program written by David Wilson of Personal
- Concepts and distributed to students of Apple's Macintosh Technical Training course.*/
-
- #include <QuickDraw.h>
- #include <MenuMgr.h>
- #include <WindowMgr.h>
- #include <TextEdit.h>
- #include <EventMgr.h>
- #include <DeskMgr.h>
- #include <ControlMgr.h>
- #include <ResourceMgr.h>
- #include <FontMgr.h>
- #include <DialogMgr.h>
- #include <PackageMgr.h>
- #include <PrintMgr.h>
- #include <MemoryMgr.h>
- #include <OSUtil.h>
- #include <ToolboxUtil.h>
- #include <FileMgr.h>
- #include <StdFilePkg.h>
-
- #define pass(A) (A)
- #define RETURN return
-
- #define NULL 0L /* 32 bit 0 or hex 0x0000 */
- #define NEG_ONE 1L
-
- #define MAXMENUS 7 /* maximum number of menus */
- #define APPLEMENU 401
- #define APPLE 0
- #define FILEMENU 402 /* resource id */
- #define FILE 1
- #define FILENEW 1 /* item number */
- #define FILEQUIT 8
- #define EDITMENU 403
- #define EDIT 2
- #define EDITCUT 3
- #define EDITCOPY 4
- #define EDITPASTE 5
- #define EDITSHOW 7
- #define Font 3
- #define fontID 404
- #define Size 4
- #define sizeID 405
- #define Pen 5
- #define penID 406
- #define penSmall 1
- #define penMedium 2
- #define penLarge 3
- #define penBlack 5
- #define penGray 6
- #define penWhite 7
- #define Picture 6
- #define pictureID 407
- #define SKETCH 1
- #define RECTANGLE 2
- #define OVAL 3
- #define TEXT 4
- #define windowID 401
- #define DIALOG_ID 401
- #define CURSOR_ID 401
- #define iBeamCursor 1 /* system resource */
-
- /* ----------------- global variables --------------------------*/
-
- GrafPtr tempPort; /* store current port during updates */
- WindowPtr theWindow, eventWindow;
- WindowRecord wRecord;
- MenuHandle myMenus[MAXMENUS]; /* array to hold menu handles */
- Rect windowRect, dragRect,textRect;
- char doneflag, temp; /* boolean */
- EventRecord myEvent; /* pointer to event record */
- int theMenu, theItem, drawMode, lastMode;
- char ch;
- CursHandle drawHandle,iBeamHdl;
- TEHandle hTE; /* handle to text edit record */
- PicHandle picHdl; /* picture handle for graphics updates */
-
- /* -------------------- Main Program --------------------------*/
-
- main()
- {
- init_stuff(); /* program initialization routine */
-
- /* ---------------------- main event loop --------------------- */
-
- while (!doneflag) { /* continue until quit is selected */
- SystemTask();
- if (theWindow == FrontWindow()) /* if our window is in front (active) */
- Cursor_adjust(); /* change to the appropriate cursor */
- if (drawMode == TEXT) TEIdle(hTE); /* blink cursor if entering text */
- if(GetNextEvent(everyEvent, &myEvent))
- switch (myEvent.what) {
-
- case mouseDown:
- Handle_mouse_down();
- break;
-
- case keyDown:
- case autoKey:
- ch = (int)(myEvent.message & charCodeMask);
- if (myEvent.modifiers & cmdKey)
- do_command(MenuKey(ch)); /* may be a command key menu equivalent */
- else
- if (theWindow == FrontWindow()) {
- if (drawMode == TEXT)
- TEKey(ch, hTE);
- else
- DrawChar(ch);
- }
- break;
-
- case activateEvt:
- if (myEvent.modifiers & 1) { /* if an activate event */
- SetPort(myEvent.message);
- TEActivate(hTE);
- }
- else /* if a deactivate event */
- TEDeactivate(hTE);
- break;
-
- case updateEvt:
- Handle_update(myEvent.message);
- break;
- }
- }
- }
-
- /* ----------- do file menu commands ------------------------ */
-
- do_file_commands(fileItem)
- int fileItem;
- {
- switch(fileItem) {
-
- case FILENEW:
- SelectWindow(theWindow); /* bring window to the front */
- ShowWindow(theWindow);
- EraseRect(&theWindow->portRect);
- break;
-
- case FILEQUIT:
- doneflag = TRUE; /* exit main event loop */
- break;
- }
- }
-
- /* ----------- do Edit menu commands ------------------------ */
-
- do_edit_commands(editItem)
- int editItem;
- {
- if (!SystemEdit(editItem-1)) { /* if a desk accessory cut, copy or paste */
- if (theWindow == FrontWindow()) /* it will be handles and this code skipped */
- switch(editItem) {
- case EDITCUT:
- if (drawMode == TEXT) TECut(hTE);
- break;
- case EDITCOPY:
- if (drawMode == TEXT) TECopy(hTE);
- break;
- case EDITPASTE:
- if (drawMode == TEXT) TEPaste(hTE);
- break;
- case EDITSHOW:
- break;
- }
- }
- }
-
- /* -------------------- Handle Font menu commands ----------------------*/
-
- do_font_commands(fontItem)
- int fontItem;
- {
- char fontName[255];
- int fontNum;
-
- GetItem(myMenus[Font], fontItem, fontName);
- GetFNum(fontName, &fontNum);
- TextFont(fontNum);
- (**hTE).txFont = fontNum;
- }
-
- /* -------------------- Handle Size menu commands ----------------------*/
-
- do_size_commands(sizeItem)
- int sizeItem;
- {
- char sizeString[255];
- long sizeNumber;
-
- GetItem(myMenus[Size], sizeItem, sizeString);
- StringToNum(sizeString, &sizeNumber);
- TextSize(LoWord(sizeNumber));
- (**hTE).txSize = LoWord(sizeNumber);
- }
-
- /* -------------------- Handle Pen menu commands ----------------------*/
-
- do_pen_commands(penItem)
- int penItem;
- {
- switch(penItem) {
-
- case penSmall:
- PenSize(1,1);
- break;
- case penMedium:
- PenSize(6,6);
- break;
- case penLarge:
- PenSize(20,20);
- break;
- case penBlack:
- PenPat(black);
- break;
- case penGray:
- PenPat(gray);
- break;
- case penWhite:
- PenPat(white);
- break;
- }
- }
-
- /* --------------- Handle Picture menu commands -------------------*/
-
- do_Pict_commands(pictItem)
- int pictItem;
- {
- drawMode = pictItem;
- CheckItem(myMenus[Picture],lastMode,FALSE); /* remove check from old item */
- CheckItem(myMenus[Picture],pictItem,TRUE); /* add check to new item */
- lastMode = pictItem;
- }
-
- /* ----------- Handle menu (and key equivalent) commands -------*/
-
- do_command(theMenu, theItem)
- int theMenu, theItem;
-
- {
- char name[255]; /* a string of 255 characters */
- int i;
-
- switch (theMenu) {
-
- case APPLEMENU:
- if (theWindow == FrontWindow())
- save_screen();
- if (theItem == 1) show_dialog(); /* if "about demo ..." selected */
- else {
- GetItem(myMenus[APPLE], theItem, name);/* get the desk accessory's name */
- OpenDeskAcc(name); /* open the desk accessory */
- }
- break;
-
- case FILEMENU:
- do_file_commands(theItem);
- break;
-
- case EDITMENU:
- do_edit_commands(theItem);
- break;
-
- case fontID:
- do_font_commands(theItem);
- break;
-
- case sizeID:
- do_size_commands(theItem);
- break;
-
- case penID:
- do_pen_commands(theItem);
- break;
-
- case pictureID:
- do_Pict_commands(theItem);
- break;
- }
- HiliteMenu(0);
- }
-
- /* -------------- sketch in window when mouse down ------------ */
-
- draw_lines()
- {
- Point p1,p2;
-
- GetMouse(&p1); /* get current mouse position */
- MoveTo(p1.h, p1.v); /* position pen to mouse position */
- while (Button()) { /* until the mouse button is released */
- GetMouse(&p2); /* get new mouse position */
- if (!EqualPt(pass(p1),pass(p2))) { /* if the two positions are different */
- LineTo(p2.h, p2.v); /* draw a line between them */
- p1.h = p2.h; /* set old position to new position */
- p1.v = p2.v;
- }
- }
- }
-
- /* ------- draw rectangles in window when mouse down -------- */
-
- draw_Rect(startPt)
- Point startPt;
- {
- Point pNew,pOld;
- Rect newRect,oldRect;
- PenState pnState;
-
- GetPenState(&pnState); /* save current pendata */
- PenMode(patXor); /* for drawing temporary outline */
- PenPat(dkGray);
- pOld = startPt;
- MoveTo(startPt.h, startPt.v);
- do {
- GetMouse(&pNew);
- if (! EqualPt(pass(pNew),pass(pOld))) {
- SetRect(&oldRect,startPt.h,startPt.v,pOld.h,pOld.v); /* define rect from two corners */
- SetRect(&newRect,startPt.h,startPt.v,pNew.h,pNew.v); /* define rect from two corners */
- FrameRect(&oldRect); /* erase temporary rectangle */
- FrameRect(&newRect); /* draw temporary rectangle */
- pOld = pNew;
- }
- } while (Button() == TRUE);
- SetPenState(&pnState); /* restore original pendata */
- FrameRect(&newRect); /* draw final rectangle */
- }
-
- /* ------- draw oval in window when mouse down -------- */
-
- draw_oval()
- {
- Point p1,p2;
- Rect tempRect;
- PenState pnState;
-
- GetPenState(&pnState); /* save current pendata */
- PenMode(patXor); /* for drawing temporary outline */
- GetMouse(&p1);
- MoveTo(p1.h, p1.v);
- do {
- GetMouse(&p2);
- Pt2Rect(pass(p1),pass(p2),&tempRect); /* define rectangle from two corners */
- FrameOval(&tempRect); /* draw temporary oval */
- FrameOval(&tempRect); /* erase temporary oval */
- } while (Button() == TRUE);
- SetPenState(&pnState); /* restore original pendata */
- FrameOval(&tempRect); /* draw final oval */
- }
-
- /* -------------- Handle all mouseDown events -------------------*/
-
- Handle_mouse_down()
- {
- WindowPtr mouseWindow;
- int location;
- Point mDown;
-
- location = FindWindow(pass(myEvent.where), &mouseWindow);
- switch(location) {
-
- case inMenuBar: /* mouse down in pull down menu */
- do_command(MenuSelect(pass(myEvent.where)));
- break;
-
- case inSysWindow: /* mousedown in desk accessory */
- SystemClick(&myEvent, mouseWindow);
- break;
-
- case inGoAway: /* mouse down in go away box */
- if (TrackGoAway(theWindow,pass(myEvent.where)))
- HideWindow(theWindow);
- break;
-
- case inDrag: /* mouse down in title bar of window */
- if (mouseWindow != FrontWindow())
- SelectWindow(mouseWindow);
- DragWindow(mouseWindow, pass(myEvent.where), &dragRect);
- break;
-
- case inContent: /* mouse down in content region of window */
- if (mouseWindow != FrontWindow())
- SelectWindow(mouseWindow);
- if (theWindow == FrontWindow())
- mDown = myEvent.where;
- GlobalToLocal(&mDown);
- switch(drawMode) {
- case SKETCH:
- draw_lines();
- break;
- case RECTANGLE:
- draw_Rect(pass(mDown));
- break;
- case OVAL:
- draw_oval();
- break;
- case TEXT:
- TEClick(pass(mDown),0,hTE);
- break;
- }
- break;
- }
- }
-
-
- /* ---------- update the window contents ----------------------*/
-
- Handle_update(updateWindow)
- WindowPtr updateWindow;
- {
- GrafPtr tempPort;
-
- GetPort(&tempPort); /* save current grafport */
- SetPort(updateWindow);
- BeginUpdate(updateWindow);
- if (drawMode == TEXT) { /* currently text mode */
- EraseRect(&theWindow->portRect);
- TEUpdate(&textRect, hTE);
- }
- else { /* currently graphics mode */
- HLock(picHdl); /* lock picture so it doesn't move */
- DrawPicture(picHdl,&textRect); /* redraw screen contents */
- HUnlock(picHdl); /* set picture relocatable again */
- }
- EndUpdate(updateWindow);
- SetPort(tempPort); /* restore original grafport */
- }
-
- /* ------- adjust the Cursor when theWindow in front -------------*/
-
- Cursor_adjust()
- {
- Point mousePt;
-
- GetMouse(&mousePt);
- if (PtInRect(pass(mousePt), &theWindow->portRect)) /* if mouse in content */
- switch(drawMode) {
- case TEXT:
- SetCursor(&(**iBeamHdl)); /* use text cursor */
- break;
- case SKETCH:
- case RECTANGLE:
- case OVAL:
- SetCursor(&(**drawHandle)); /* use graphics cursor */
- break;
- }
- else
- InitCursor(); /* use arrow cursor */
- }
-
- /* ------------------ initialize menus --------------------------*/
-
- set_up_menus()
- {
- InsertMenu(myMenus[APPLE] = GetMenu(APPLEMENU),0);
- AddResMenu(myMenus[APPLE], 'DRVR'); /* get desk accessory names */
- InsertMenu(myMenus[FILE] = GetMenu(FILEMENU),0);
- InsertMenu(myMenus[EDIT] = GetMenu(EDITMENU),0);
- InsertMenu(myMenus[Font] = GetMenu(fontID),0);
- AddResMenu(myMenus[Font], 'FONT');
- InsertMenu(myMenus[Size] = GetMenu(sizeID),0);
- InsertMenu(myMenus[Pen] = GetMenu(penID),0);
- InsertMenu(myMenus[Picture] = GetMenu(pictureID),0);
- DrawMenuBar();
- CheckItem(myMenus[Picture],TEXT,TRUE); /* put check mark by text item */
- lastMode = TEXT; /* set previous mode to text */
- }
-
- /* ---------------- modal dialog box -----------------------*/
-
- show_dialog()
- {
- DialogPtr dp;
- int ditem;
-
- dp = GetNewDialog(DIALOG_ID,NULL,NEG_ONE);
- ModalDialog(NULL,&ditem);
- DisposDialog(dp);
- }
-
- /* ------------- save screen for graphics update --------------- */
-
- save_screen()
- {
- BitMap map;
-
- KillPicture(picHdl); /* reclaim and reuse pic memory in heap */
- ClipRect(&screenBits.bounds); /* limit clip region to size of screen */
- map = theWindow->portBits; /* the bitmap for the graphics/text window */
- picHdl = OpenPicture(&textRect); /* prepare to capture screen image */
- CopyBits(&map,&map,&textRect,&textRect,1,NULL); /* copy window to itself */
- ClosePicture(); /* stop accumulating picture */
- }
-
- init_stuff()
- {
- MoreMasters(); /* make 64 more Master Pointers */
- MoreMasters(); /* make 64 more Master Pointers */
- MoreMasters(); /* make 64 more Master Pointers */
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL); /* error handling proc goes here */
- InitCursor();
-
- OpenResFile("\Pdemo9.rsrc");
- set_up_menus();
-
- windowRect.left = screenBits.bounds.left + 8; /* rect for main text window */
- windowRect.top = screenBits.bounds.top + 40;
- windowRect.right = screenBits.bounds.right - 8;
- windowRect.bottom = screenBits.bounds.bottom - 8;
- theWindow = NewWindow(NULL, &windowRect,"\PDemo in C", 1, 0, NEG_ONE, 1, NULL);
- SetPort(theWindow);
-
- dragRect = windowRect; /* for dragging windows around */
- InsetRect(&dragRect,-4,-4);
-
- textRect = theWindow->portRect; /* text edit stuff */
- InsetRect(&textRect,6,10); /* destination & view rectangles */
- hTE = TENew(&textRect, &textRect);
-
- drawHandle = GetCursor(CURSOR_ID); /* miscellaneous initializing */
- iBeamHdl = GetCursor(iBeamCursor);
- doneflag = FALSE;
- FlushEvents(everyEvent, 0);
- drawMode = TEXT;
- picHdl = NULL; /* zero handle so first update */
- } /* won't blow up the program */
-